package com.onlyphoto.onlymall.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.util.Set;


public class SharePreferenceUtil {

    private static SharePreferenceUtil util;
    private static SharedPreferences share;
    private SharedPreferences.Editor mEditor;
    private static final String VALUE = "session_id";
    private static final String USER_NAME = "user_name";

    public SharePreferenceUtil(Context context) {
        share = context.getSharedPreferences("loan_Share", Context.MODE_PRIVATE);
        mEditor = share.edit();
    }

    public static SharePreferenceUtil getInstance(Context context) {
        if (util == null) {
            util = new SharePreferenceUtil(context);
        }
        return util;
    }

    public void setData(String key, String data) {
        if (TextUtils.isEmpty(key)) {
            return;
        }
        mEditor.putString(key, data);
        mEditor.commit();
    }

    public String getData(String key) {
        return share.getString(key, "");
    }

    public void setIntData(String key, int data) {
        if (TextUtils.isEmpty(key)) {
            return;
        }
        mEditor.putInt(key, data);
        mEditor.commit();
    }

    public void setLong(String key, long value) {
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    public long getLong(String key, long defaultValue) {
        return share.getLong(key, defaultValue);
    }

    public int getIntData(String key, int defValue) {
        return share.getInt(key, defValue);
    }

    public void setBoolData(String key, boolean data) {
        if (TextUtils.isEmpty(key)) {
            return;
        }
        mEditor.putBoolean(key, data);
        mEditor.commit();
    }

    public boolean getBoolData(String key, boolean defValue) {
        return share.getBoolean(key, defValue);
    }


    public void setStringSet(String key, Set<String> stringSet) {
        mEditor.putStringSet(key, stringSet);
        mEditor.commit();
    }

    public Set<String> getStringSet(String key, @Nullable Set<String> defaultSet) {
        return share.getStringSet(key, defaultSet);
    }

    public String getToken() {
        return getData(VALUE);
    }

    public void setToken(String token) {
        setData(VALUE, token);
    }

    public String getUserName() {
        return getData(USER_NAME);
    }

    public void setUserName(String userName) {
        setData(USER_NAME, userName);
    }
}
